package com.wn.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

public class CookieUtils {

    public static String getCookie(String name, HttpServletRequest httpServletRequest){
        Cookie[] cookies = httpServletRequest.getCookies();
        if (Objects.nonNull(cookies)){
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(name)){
                    return cookie.getValue();
                }
            }
        }
        return null;
    }

    public static void setCookie(String name, String value , HttpServletResponse httpServletResponse){
        Cookie cookie = new Cookie(name,value);
        //-- 一定 一定 一定 一定要设置Path为 /
        cookie.setPath("/");
        httpServletResponse.addCookie(cookie);
    }
}
